//
//  ShareComposePhotos.m
//  ArtClouds
//
//  Created by 景 on 15/10/9.
//  Copyright © 2015年 lishuai. All rights reserved.
//

#import "ShareComposePhotos.h"
#import "UIView+Helper.h"
@interface ShareComposePhotos()
@end

@implementation ShareComposePhotos

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _photos = [NSMutableArray array];
    }
    return self;
}

- (void)addPhoto:(UIImage *)photo
{
    UIImageView *photoView = [[UIImageView alloc] init];
    photoView.image = photo;
    //添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    [tap addTarget:self action:@selector(deleteImage:)];
    photoView.userInteractionEnabled = YES;
    [photoView addGestureRecognizer:tap];


    [self addSubview:photoView];
    
//    UIButton *button = [[UIButton alloc] init];
//    [button setBackgroundImage:[UIImage imageNamed:@"error"] forState:UIControlStateNormal];
    UIImageView *iconView = [[UIImageView alloc] init];
    [iconView setImage:[UIImage imageNamed:@"error"]];
    [photoView addSubview:iconView];
    //存储图片
    [self.photos addObject:photoView];
    

}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //设置图片的尺寸和位置
    NSUInteger count = self.subviews.count;
    int maxCol = 3;
    
    CGFloat imageMargin = 10;
    
    CGFloat imageWH = (self.width - 4 * imageMargin) / 3;
    
    for (int i = 0; i < count; i++) {
        UIImageView *photoView = self.subviews[i];
        photoView.tag = i + 20;
//        UIButton *button = photoView.subviews[0];
//        button.frame = CGRectMake(50, 0, 20, 20);
//        button.tag = i;
        UIImageView *iconView = photoView.subviews[0];
        iconView.frame = CGRectMake(50, 0, 20, 20);
//        iconView.tag = i;



        int col = i % maxCol;
        photoView.x = col * imageWH + imageMargin * (col + 1);
        
        int row = i / maxCol;
        photoView.y = row * (imageWH +imageMargin);
        photoView.width = imageWH;
        photoView.height = imageWH;
    }
}

//删除已选图片
- (void)deleteImage:(UITapGestureRecognizer *)tapGesture
{
    UIImageView *deleteImageView = (UIImageView *)tapGesture.view;
    [deleteImageView removeFromSuperview];
    [_photos removeObjectAtIndex:deleteImageView.tag - 20];
    

}


@end
